﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2009-10-15
//
// Update Date :            
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


//----------------------------------------------------------------
// 阵列网格中的单个对象
//----------------------------------------------------------------
struct FAPI FU_AG_GRID
{
    FUI_DECLARE_STRUCT(FU_AG_GRID);

    bool bNeedDraw, bDisable, bMouseOver, bSelected;
    DWORD dwID;
    DWORD_PTR dwDataPtr;
    DWORD dwUD;

    FINLINE FU_AG_GRID()
    {
        bSelected = bDisable = bMouseOver = false; bNeedDraw = true, Clear();
    }
    FINLINE void Clear()
    {
        dwID = 0, dwDataPtr = 0, dwUD = 0;
    }
};
typedef FUI_Vector< FU_AG_GRID > FV_UI_AG_GRID;


//----------------------------------------------------------------
// 阵列网格的单个格子对象自定义渲染
// 注意区分 non_text 参数， non_text为true时画图片，为false时画文字，以提高效率
//----------------------------------------------------------------
class FUI_ArrayGrid;
typedef void (*FUI_ArrayGrid_Render)(FUI_ArrayGrid* pctrl, FU_AG_GRID* pData, int nX, int nY, fuRect* rc, bool non_text); 


//----------------------------------------------------------------
//
//----------------------------------------------------------------
class FAPI FUI_ArrayGrid : public FUI_Object
{
    FUI_DECLARE_CLASS(FUI_ArrayGrid)

public:
    FUI_ArrayGrid(EF_UI_WIDGET_TYPE t = eUWT_ArrayGrid);
    virtual void Render(EF_UI_RENDER_LAYER layer);
    virtual bool OnLButtonUp(funit nX, funit nY);
    virtual bool OnMouseMove(funit nX, funit nY);
    virtual bool GetCursorResult(EF_UI_EVENT_TYPE t){ return false;}

    // 获取指定位置格子数据
    virtual FU_AG_GRID* GetGrid(int index);
    virtual FU_AG_GRID* GetGrid(int x, int y);
    // 取得总格子数量
    virtual size_t GetCount(){ return pArrayGridData->nArrayCountX * pArrayGridData->nArrayCountY; }
    // 横方向上格子对象数量
    virtual int GetColCount(){ return pArrayGridData->nArrayCountX; }
    // 纵方向上格子对象数量
    virtual int GetRowCount(){ return pArrayGridData->nArrayCountY; }
    // 清空格子数据
    virtual void Clear();
    // 取消当前选中格子
    virtual void CancelSelect();
    // 取消当前悬停格子
    virtual void CancelMouseOver();

    // 设置格子自定义绘制函数
    FINLINE void SetRenderFunc(FUI_ArrayGrid_Render pfunc){ pGridRender = pfunc; }
    // 取得当前选中格子
    FINLINE FU_AG_GRID* GetSelGrid(){ return pCurSelItem; }
    // 取得当前鼠标悬停格子
    FINLINE FU_AG_GRID* GetMoveOnGrid(){ return pCurMoveOnItem; }

protected:
    virtual void InitUIData(FUI_Object* father, fuWidgetBase* pData, FUI_Layout* layout);
    // 返回值表示是否结束循环
    typedef bool (FUI_ArrayGrid::*AG_WorkFunc)(FU_AG_GRID* data, fuRect* dst, int nX, int nY);
    // 处理函数
    bool forWorkRenderNonText(FU_AG_GRID* data, fuRect* dst, int nX, int nY);
    bool forWorkRenderWithText(FU_AG_GRID* data, fuRect* dst, int nX, int nY);
    bool forWorkMouseMove(FU_AG_GRID* data, fuRect* dst, int nX, int nY);
    bool forWorkLButtonUp(FU_AG_GRID* data, fuRect* dst, int nX, int nY);
    // 循环处理器
    bool forWorkAGProcess(AG_WorkFunc pwork);

protected:
    fuWidgetArrayGrid* pArrayGridData;  // 控件数据地址
    FV_UI_AG_GRID vvGridData;            // 格子列表
    FU_AG_GRID* pCurSelItem;            // 当前选中项
    FU_AG_GRID* pCurMoveOnItem;         // 当前鼠标所指项
    FUI_ArrayGrid_Render pGridRender;   // 自定义绘制函数
};